import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/bean/user.dart';
import 'package:app/src/http/model/submit_model.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/image/ui_image_upload.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../../http/bean/worksdo/equipment_management_bean.dart';
import '../../../../../../http/model/equipment_management_model.dart';
import '../../../../../../http/model/hardware_collar_model.dart';

///审核提交
class ReviewSubmittedStateful3 extends StatefulWidget {
  //数据
  EquipmentResponseData? mData = null;

  //提交之前回调,用于提交参数SubData赋值。
  ResponseCallbackWithT<EquipmentResponseData>? _callbackWithT = null;

  //提交前；用于验证数据。
  ResponseCallbackWithBoolResultII? _callbackWithBoolResultII = null;

  ResponseCallbackWithBool? _responseCallbackWithBool = null;

  ReviewSubmittedStateful3(this.mData,
      {ResponseCallbackWithT<EquipmentResponseData>? callbackWithT = null,
      ResponseCallbackWithBoolResultII? callbackWithBoolResult = null,
      ResponseCallbackWithBool? responseCallbackWithBool}) {
    this._callbackWithT = callbackWithT;
    this._callbackWithBoolResultII = callbackWithBoolResult;
    this._responseCallbackWithBool = responseCallbackWithBool;
  }

  @override
  _State createState() => _State();
}

class _State extends State<ReviewSubmittedStateful3> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter?.mData = widget?.mData;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    AuditManager.getAuditManager()
        .setDetailsWithAuto(false, "review_submitted.dart"); //fixme 不在审核详情页面。
  }

  @override
  Widget build(BuildContext context) {
    // AuditManager.setDetailsWithAuto(
    //     true, "review_submitted.dart"); //fixme 当前在审核详情页面。现在需求任然需要显示通知信息。。
    return _body(context);
  }

  Widget _body(BuildContext context) {
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      presenter.setStateBody = setState;
      List<Widget> children = [];
      var topMargin = 16.0;

      // children.add(_EstimatedWorkingHours(context)); //预估工时
      var submitName = AppStrings.of(context).receive; //领用
      var SubmitAudit = MaterialButton(
        onPressed: () {
          var b = true;
          if (widget._callbackWithBoolResultII != null) {
            var b2 =
                widget._callbackWithBoolResultII?.call(); //fixme 提交之前验证数据格式。
            if (b2 != null) {
              b = b2;
            }
          }
          if (b) {
            presenter?.contractapply(context, false, widget._callbackWithT,
                widget._responseCallbackWithBool);
          }
        },
        color: ColorUtils.parseColor("#5985F0"),
        textColor: Colors.white,
        padding: EdgeInsets.all(15),
        elevation: 0,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8), bottomLeft: Radius.circular(8))),
        child: Text(submitName),
      );
      //返回列表
      var ReturnsList = MaterialButton(
        onPressed: () {
          AppRouteHelper.pop(context);
        },
        color: ColorUtils.parseColor("#8F97AC"),
        textColor: Colors.white,
        padding: EdgeInsets.all(15),
        elevation: 0,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                topRight: Radius.circular(8), bottomRight: Radius.circular(8))),
        child: Text(AppStrings.of(context).ReturnsList),
      );
      var row = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [Expanded(child: SubmitAudit), Expanded(child: ReturnsList)],
      );
      children.add(row);

      return Container(
        padding: EdgeInsets.only(left: 8, right: 8, top: topMargin, bottom: 16),
        child: Column(
          children: children,
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
                color: Colors.black, blurRadius: 10, offset: Offset(0, 10))
          ],
        ),
      );
    });
  }
}

class _Presenter {
  ///body视图状态
  StateSetter? setStateBody;

  TextEditingController? textEditingController = null;

  EquipmentResponseData? mData = null;

  ///提交
  ///IsConfirm 是否确认二次提交；二次确认提交，code 为 101
  contractapply(
      BuildContext context,
      bool IsConfirm,
      ResponseCallbackWithT<EquipmentResponseData>? _callbackWithT,
      ResponseCallbackWithBool? _responseCallbackWithBool) {
    AuditManager.getAuditManager().setDetailsWithAuto(true,
        "review_submitted2.dart"); //fixme 在审核详情页面,正在提交的时候，不要显示通知信息。没有提交的时候，还是要显示的。
    _callbackWithT?.call(mData!); //fixme 提交之前回调，用于提交参数赋值。
    //fixme 审核提交
    HardwareCollarModel.AddMaterialPickBack(context, mData, IsConfirm, true)
        ?.then((value) {
      if (value == null) {
        return;
      }
      if (value.isSuccessForCode()) {
        Toast.showSuccess(
            AppStrings.of(context).SubmitSuccessfully, context); //提交成功
        AppRouteHelper.pop(context, true); //fixme 关闭当前审核页面
        AuditManager.getAuditManager().setDetailsWithAuto(
            false, "review_submitted2.dart"); //fixme 审核详情页面提交结束，可以显示通知信息。
        // _responseCallbackWithBool?.call(true);//fixme 不关闭当前页面，回调主页面，刷新，重新获取数据。
      } else if (value.isSuccessForCode101WithHardware()) {
        //二次提升框:确认申请
        AppDialog.showDialogWithAlert(context,
                content: value.MsgDetail,
                okText: AppStrings.of(context).Confirmtheapplication)
            ?.then((value) {
          if (value ?? false) {
            contractapply(
                context, true, _callbackWithT, _responseCallbackWithBool);
          }
        });
        AuditManager.getAuditManager().setDetailsWithAuto(
            false, "review_submitted2.dart"); //fixme 审核详情页面提交结束，可以显示通知信息。
      } else {
        //提示错误信息
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail);
        AuditManager.getAuditManager().setDetailsWithAuto(
            false, "review_submitted2.dart"); //fixme 审核详情页面提交结束，可以显示通知信息。
        _responseCallbackWithBool?.call(false);
      }
      //100 提升成功
      //非100 提升错误信息
      //101 二次确认弹窗
    });
  }
}
